import 'package:hongsheng/api/SPClassApiManager.dart';
import 'package:hongsheng/api/SPClassHttpCallBack.dart';
import 'package:hongsheng/app/SPClassApplicaion.dart';
import 'package:hongsheng/model/SPClassPointsProductEntity.dart';
import 'package:hongsheng/utils/SPClassCommonMethods.dart';
import 'package:hongsheng/utils/SPClassImageUtil.dart';
import 'package:hongsheng/utils/SPClassNavigatorUtils.dart';
import 'package:hongsheng/utils/SPClassStringUtils.dart';
import 'package:hongsheng/utils/colors.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../SPClassSignInPage.dart';
import 'IntegralProductDetail.dart';
import 'SPClassExchangeRecordPage.dart';
import 'SPClassInputInfo.dart';
import 'SPClassIntegralDetail.dart';

///积分商城
class SPClassIntegralShopPage extends StatefulWidget {
  const SPClassIntegralShopPage({Key ?key}) : super(key: key);

  @override
  _SPClassIntegralShopPageState createState() => _SPClassIntegralShopPageState();
}

class _SPClassIntegralShopPageState extends State<SPClassIntegralShopPage> {
  List<SPClassProductCategory> tabBarList = [
  ];

  List<SPClassProduct> spProProductList = [];


  int index =0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    SPClassApplicaion.spProEventBus.on<String>().listen((event) {
      if(event=="loginInfo"){
        if(mounted){
          setState(() {
          });
        }
      }
    });

    getProduct(canBuy: 1);


  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:  AppBar(
        elevation: 0,
        leading: FlatButton(
          child: Icon(Icons.arrow_back_ios,size: width(20),color: Colors.white,),
          onPressed: (){Navigator.of(context).pop();},
        ),
        centerTitle: true,
        title: Text("积分商城",style: TextStyle(fontSize:  sp(19)),),
        actions: <Widget>[
          GestureDetector(
            child: Row(
              children: <Widget>[
                Image.asset(
                  SPClassImageUtil.spFunGetImagePath(
                      "ic_clock"),
                  width: width(15),
                ),
                Text('兑换记录',style: TextStyle(fontSize: sp(13)),),
                SizedBox(width: width(10),)
              ],
            ),
            behavior: HitTestBehavior.translucent,
            onTap: (){
              SPClassNavigatorUtils.spFunPushRoute(context, SPClassExchangeRecordPage());
            },
          )
        ]
      ),
      body: Column(
        children: <Widget>[
          Container(
            color: Colors.white,
            height: width(48),
            padding: EdgeInsets.symmetric(horizontal: width(10)),
            child: Row(
              children: <Widget>[
                // Text('我的积分',style: TextStyle(fontSize: sp(13),color: MyColors.grey_99),),
                Image.asset(
                  SPClassImageUtil.spFunGetImagePath('ic_jifen',),
                  width: width(30),
                ),
                SizedBox(width: width(2),),
                Text('${SPClassApplicaion.spProUserInfo!.spProPoints}',style: TextStyle(fontSize: sp(19),color: MyColors.main1,height: 1.2),),
                Expanded(
                  child: SizedBox(),
                ),
                GestureDetector(
                  onTap: (){
                    SPClassNavigatorUtils.spFunPushRoute(context, SPClassIntegralDetail());
                  },
                  child: Container(
                    width: width(72),
                    height: width(25),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: MyColors.main1,
                        borderRadius: BorderRadius.circular(150)
                    ),
                    child: Text('积分明细',style: TextStyle(fontSize: sp(12),color: Colors.white),),
                  ),
                ),
                SizedBox(width: width(15),),
                GestureDetector(
                  onTap: (){
                    SPClassNavigatorUtils.spFunPopAll(context);
                    SPClassNavigatorUtils.spFunPushRoute(context, SPClassSignInPage());
                  },
                  child: Container(
                    width: width(72),
                    height: width(25),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: MyColors.main1,
                        borderRadius: BorderRadius.circular(150)
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text('赚积分',style: TextStyle(fontSize: sp(12),color: Colors.white),),
                        Image.asset(
                          SPClassImageUtil.spFunGetImagePath(
                              "ic_right_sanjiao"),
                          width: width(12),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: Container(
              decoration: BoxDecoration(
                  border: Border(top: BorderSide(width: 1,color: Color(0xFFF2F2F2)))
              ),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Container(
                      color: Color(0xFFF2F2F2),
                      child: SingleChildScrollView(
                        child: Column(
                          children: tabBarList.map((e) {
                            return GestureDetector(
                              onTap: (){
                                index = tabBarList.indexOf(e);
                                setState(() {
                                });
                                getProduct(categoryId:tabBarList[index].spProCategoryId,canBuy: index==0?1:0 );
                              },
                              child: Container(
                                height: width(52),
                                alignment: Alignment.center,
                                color: index==tabBarList.indexOf(e)?Colors.white:Colors.transparent,
                                child: Text(e.spProCategoryName!,style: TextStyle(fontSize: sp(13),color:index==tabBarList.indexOf(e)?MyColors.main1:MyColors.grey_33 ),),
                              ),
                            );
                          }).toList(),
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 3,
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                      ),
                      child: SingleChildScrollView(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            Container(
                              child: Image.asset(
                                SPClassImageUtil.spFunGetImagePath(
                                    "ic_jifen_banner"),
                                width: MediaQuery.of(context).size.width,
                              ),
                              margin: EdgeInsets.only(left: width(10),right: width(10),top: width(10)),
                            ),
                            SizedBox(height: width(23),),
                            Column(
                              children: spProProductList.map((e) {
                                return shopItem(e);
                              }).toList(),
                            ),

                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          )
        ],
      ),

    );
  }

  Widget shopItem(SPClassProduct data) {
    return data.spProIsOpen=='0'?Container():
    GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: (){
        // SPClassNavigatorUtils.spFunPushRoute(context, IntegralProductDetail(data: data,));
      },
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: width(10)),
        margin: EdgeInsets.only(bottom: width(23)),
        child: Row(
          children: <Widget>[
            ClipRRect(
              borderRadius: BorderRadius.circular(4),
              child: Image.network(
                data.spProIcon!,
                fit: BoxFit.cover,
                width: width(61),
              ),
            ),
            SizedBox(width: width(8),),
            Expanded(
              child: Container(
                height: width(61),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(data.spProProductName??'',style: TextStyle(fontSize: sp(13),color: MyColors.grey_33,fontWeight: FontWeight.bold),overflow: TextOverflow.ellipsis,),
                    Text(data.spProDescribe??'',style: TextStyle(fontSize: sp(12),color: MyColors.grey_99,),overflow: TextOverflow.ellipsis,),
                    Expanded(
                      child: SizedBox(),
                    ),
                    Text('限时${SPClassStringUtils.optimizePrice(double.parse(data.spProPrice!))}积分',style: TextStyle(fontSize: sp(12),color: MyColors.main1,),),
                  ],
                ),
              ),
            ),
            SizedBox(width: width(8),),
            Container(
              height: width(61),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  Text('剩余:${data.spProNum}',style: TextStyle(fontSize: sp(10),color: MyColors.grey_99),),
                  SizedBox(height: width(4),),
                  GestureDetector(
                    onTap: (){
                      if(data.spProNum=='null'||data.spProNum=='0'){
                        return;
                      }
                      if(data.spProType=='3'){
                        // 标记
                        // SPClassNavigatorUtils.spFunPushRoute(context, SPClassLootProductDetail(data.spProProductId));
                      }else{
                        SPClassNavigatorUtils.spFunPushRoute(context, SPClassInputInfo(data: data,));
                      }
                    },
                    child: Container(
                      width: width(46),
                      height: width(23),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: (data.spProNum=='null'||data.spProNum=='0')?MyColors.grey_cc:MyColors.main1,
                        borderRadius: BorderRadius.circular(150),
                      ),
                      child: Text((data.spProNum=='null'||data.spProNum=='0')?'缺货中':data.spProType=='3'?'去夺宝':'兑换',style: TextStyle(fontSize: sp(12),color: Colors.white),),
                    ),
                  )
                ],
              ),
            )

          ],
        ),
      ),
    );
  }

  void getProduct({String ?categoryId,int canBuy=0}){

    Map<String, dynamic> params={};
    params = {
      "category_id":categoryId,
      'i_can_buy':canBuy,
    };
    SPClassApiManager.spFunGetInstance().spFunGetPointProduct<SPClassPointsProductEntity>(params: params,spProCallBack:SPClassHttpCallBack(
        spProOnSuccess: (result){
          if(tabBarList.isEmpty){
            Map<String ,dynamic> map ={'category_name':'我能兑换',};
            tabBarList.add(SPClassProductCategory.fromJson(map));
            tabBarList.addAll(result.spProCategory!) ;
          }
          spProProductList =result.spProProduct!;
          setState(() {

          });
        }
    ));
  }



}
